package com.zmn.brs.admin.controller.rule.objective;

import com.zmn.brs.model.vo.rule.ObjRuleVO;
import com.zmn.brs.services.interfaces.threshold.ThresholdService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;


/**
 * 描述： 绩效目标规则
 * @author chongw
 * @since 2021-05-28 17:05:29
 */
@RestController
@RequestMapping("/brs/objective/rule/")
public class ObjectiveRuleModifyController extends ObjectiveRuleBaseController {

    @Resource
    private ThresholdService thresholdService;

    /**
     * 保存规则配置
     * @param objRuleVO
     * @return
     */
    @ResponseBody
    @RequestMapping("modifyRule")
    public ResponseDTO modifyRule(@RequestBody ObjRuleVO objRuleVO){
        try {
            if (NumberUtil.isNullOrZero(objRuleVO.getThresholdId())) {
                return ResponseDTO.fail("参数不能为空");
            }
            if (!NumberUtil.isPositiveInteger(objRuleVO.getValue1())) {
                return ResponseDTO.fail("一级预警值必须为正整数");
            }
            if (!NumberUtil.isPositiveInteger(objRuleVO.getValue2())) {
                return ResponseDTO.fail("二级预警值必须为正整数");
            }
            if (!NumberUtil.isPositiveInteger(objRuleVO.getValue3())) {
                return ResponseDTO.fail("三级预警值必须为正整数");
            }
            if (objRuleVO.getValue2() > objRuleVO.getValue1()) {
                return ResponseDTO.fail("二级预警值不能大于一级预警值");
            }
            if (objRuleVO.getValue3() > objRuleVO.getValue2()) {
                return ResponseDTO.fail("三级预警值不能大于二级预警值");
            }
            objRuleVO.setMcStaffName(super.getMcStaffName());
            Integer flag = thresholdService.updateRuleThreshold(objRuleVO);
            if(flag > 0){
                return ResponseDTO.success();
            }
        }catch (Exception e){
            logger.error("保存规则配置异常，异常信息：" + e);
            e.printStackTrace();
        }
        return ResponseDTO.fail("保存规则配置失败");
    }
    
}
